package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishflavorMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishflavorMapper dishflavorMapper;
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        Long id = dish.getId();
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        if(flavorList != null && flavorList.size() > 0)
        {
            for (DishFlavor dishFlavor : flavorList) {
                dishFlavor.setDishId(id);
            }
            dishflavorMapper.insertBatch(flavorList);
        }

    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteById(List<Long> ids) {
        for (Long id : ids) {
            Dish dish=dishMapper.findById(id);
            if(dish.getStatus() == StatusConstant.ENABLE)
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishflavorMapper.deleteById(id);
        }
    }

    @Override
    public DishVO findById(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish = dishMapper.findById(id);
        log.info("菜品信息：{}",dish);
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishflavorMapper.findById(id));
        return dishVO;
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishflavorMapper.deleteById(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0)
        {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishflavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public List<DishVO> list(Long categoryId) {
        List<DishVO> dishVOS = dishMapper.findByListcategoryId(categoryId);
        return dishVOS;
    }

    public List<DishVO> listWithFlavor(Dish dish) {


        List<DishVO> dishVOList =dishMapper.findByListcategoryId(dish.getCategoryId());

        for (DishVO dishVO : dishVOList) {


            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishflavorMapper.findById(dish.getId());

            dishVO.setFlavors(flavors);
        }

        return dishVOList;
    }


}